package cn.iocoder.yudao.module.xkfz.service.talentEchelon.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.xkfz.domain.entity.basic.IndicatorTree;
import cn.iocoder.yudao.module.xkfz.domain.entity.rules.ScoreCalculationData;
import cn.iocoder.yudao.module.xkfz.domain.entity.talentEchelon.TalentEchelon;
import cn.iocoder.yudao.module.xkfz.domain.excel.entity.GuidelinesAndConsensus;
import cn.iocoder.yudao.module.xkfz.mapper.basic.IndicatorTreeMapper;
import cn.iocoder.yudao.module.xkfz.mapper.talentEchelon.TalentEchelonMapper;
import cn.iocoder.yudao.module.xkfz.service.reporting.ScoreCalculationService;
import cn.iocoder.yudao.module.xkfz.service.talentEchelon.TalentEchelonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class TalentEchelonServiceImpl implements TalentEchelonService {

    @Autowired
    TalentEchelonMapper talentEchelonMapper;

    @Autowired
    IndicatorTreeMapper indicatorTreeMapper;

    @Autowired
    private ScoreCalculationService scoreCalculationService;
    @Override
    public Boolean importIntroductionOfCulture(List<TalentEchelon> list, String year,String indicatorName) {
        if (CollectionUtil.isNotEmpty(list)){
            list = list.stream().filter(talentEchelon -> indicatorName.equals(talentEchelon.getIndicatorName())).collect(Collectors.toList());
            list.forEach(res->{
               res.setYear(year);
               res.setIndicatorName(indicatorName);
            });
            //删除原库数据
            talentEchelonMapper.delete(
                    new LambdaQueryWrapperX<TalentEchelon>()
                            .eq(TalentEchelon::getYear,year)
                            .eq(TalentEchelon::getIndicatorName, indicatorName)
            );
            //新增导入数据
            talentEchelonMapper.insertBatch(list);
            //计算分数
            IndicatorTree indicatorTree = indicatorTreeMapper.selectOne(
                    new LambdaQueryWrapperX<IndicatorTree>()
                            .eq(IndicatorTree::getYear,year)
                            .eq(IndicatorTree::getIndicatorName, indicatorName));
            //分类，求每个科室，每一个类型的个数
            List<ScoreCalculationData> scoreCalculationData = talentEchelonMapper.queryTalent(year);
            scoreCalculationService.selectionRule(String.valueOf(indicatorTree.getId()), year, scoreCalculationData);
            return true;
        }


//        if (CollectionUtil.isNotEmpty(list)){
//            for (GuidelinesAndConsensus guidelinesAndConsensus : list) {
//                guidelinesAndConsensus.setPublishYear(year);
//            };
//            if (guidelinesAndConsensusMapper.insertBatch(list,500)) {
//                addScoreData(year);
//            }
//        }
//        return true;
        return null;
    }
}
